package com.xxwy.util;

import io.netty.util.Recycler;

/**
 * @author wy
 * @date 2020/3/20 14:08
 */
public class RecycleTest {

  private static class User {
    private int age;

    public int getAge() {
      return age;
    }

    public void setAge(int age) {
      this.age = age;
    }

    public Recycler.Handle<User> getHandler() {
      return handler;
    }

    private final Recycler.Handle<User> handler;

    private User(Recycler.Handle<User> handler) {
      this.handler = handler;
    }

    public void recycle() {
      handler.recycle(this);
    }

    @Override
    public String toString() {
      return "User{" +
          "age=" + age +
          '}' + super.toString();
    }
  }

  private static final Recycler<User> RECYCLER = new Recycler<User>() {
    @Override
    protected User newObject(Handle<User> handle) {
      return new User(handle);
    }
  };

  public static void main(String[] args) {
    User user = RECYCLER.get();
    user.setAge(15);
    System.out.println(user);//User{age=15}com.xxwy.util.RecycleTest$User@23fc625e

    user.recycle();
    User user2 = RECYCLER.get();
    System.out.println(user2);//User{age=15}com.xxwy.util.RecycleTest$User@23fc625e


  }
}
